package com.jeesite.modules.dataScreen.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesite.modules.dataScreen.service.OfficeDataScreenService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class OfficeDataScreenUtils {

    /**
     * 获取“开工项目”、“开工里程”柱形图
     */
    public static final String CACHE_START_PROJECT_MILEAGE_DATA = "CACHE_START_PROJECT_MILEAGE_DATA";

    /**
     * 获取“通车项目”、“通车里程”柱形图
     */
    public static final String CACHE_OPEN_PROJECT_MILEAGE_DATA = "CACHE_OPEN_PROJECT_MILEAGE_DATA";

    /**
     * 获取“建设里程(行政/技术)”柱形图
     */
    public static final String CACHE_CONSTRUCTION_MILEAGE_DATA = "CACHE_CONSTRUCTION_MILEAGE_DATA";

    /**
     * 获取“当日入口流量”实时数据
     */
    public static final String CACHE_HIGHWAY_ENTRANCE_FLOW_DATA = "CACHE_HIGHWAY_ENTRANCE_FLOW_DATA";

    /**
     * 获取“当日入口流量”实时数据
     */
    public static final String CACHE_HIGHWAY_EXIT_FLOW_DATA = "CACHE_HIGHWAY_EXIT_FLOW_DATA";

    /**
     * 获取“当日在网车辆”实时数据
     */
    public static final String CACHE_ROAD_NETWORK_DATA = "CACHE_ROAD_NETWORK_DATA";
    public static final String CACHE_OFFICE_DATA_SCREEN_LIST_BY_KEYS = "CACHE_OFFICE_DATA_SCREEN_LIST_BY_KEYS";
    public static final String CACHE_OFFICE_DATA_SCREEN_ALL_LIST = "CACHE_OFFICE_DATA_SCREEN_ALL_LIST";
    /**
     * 投资规模缓存key
     */
    private static final String CACHE_INVESTMENT_DATA = "CACHE_INVESTMENT_DATA";
    /**
     * 突发事件缓存key
     */
    private static final String CACHE_EMERGENCIES_DATA = "CACHE_EMERGENCIES_DATA";
    /**
     * 交通指数缓存key
     */
    private static final String CACHE_TRAFFIC_DATA = "CACHE_TRAFFIC_DATA";
    /**
     * 管制情况缓存key
     */
    private static final String CACHE_REGULATORY_DATA = "CACHE_REGULATORY_DATA";

    /**
     * 获取 投资规模 环形图数据
     *
     * @param year 年份
     */
    public static Map<String, List<Map<String, Object>>> getInvestmentScaleData(String year) {
        // 1.从缓存中获取
        Map<String, List<Map<String, Object>>> investmentData = CacheUtils.get(CACHE_INVESTMENT_DATA, year);
        // 2.判断是否获取成功
        if (MapUtil.isEmpty(investmentData)) {
            // 3.从数据库拿数据
            List<Map<String, Object>> planData = Static.service.getPlanInvestmentScaleData(year);
            List<Map<String, Object>> accumulData = Static.service.getAccumulInvestmentScaleData(year);
            investmentData = MapUtil.newHashMap();
            // 4.封装
            investmentData.put("planChart", planData);
            investmentData.put("accumulChart", accumulData);
            CacheUtils.put(CACHE_INVESTMENT_DATA, year, investmentData);
        }
        return investmentData;
    }

    /**
     * 获取 突发事件 环形图数据
     */
    public static List<Map<String, Object>> getEmergenciesData() {
        //1.从缓存中拿数据
        List<Map<String, Object>> emergenciesData = SysCacheUtils.get(CACHE_EMERGENCIES_DATA);
        //2.判断是否拿到
        if (CollUtil.isEmpty(emergenciesData)) {
            //3.从数据库拿数据然后存入缓存
            emergenciesData = Static.service.getEmergenciesData();
            //获取封装后的数据
            List<Map<String, Object>> newData = EmergenciesUtils.sortEmergencies(emergenciesData);
            emergenciesData = newData;
            SysCacheUtils.put(CACHE_EMERGENCIES_DATA, newData);
        }
        return emergenciesData;
    }

    public static Map<String, Object> getTrafficIndexData() {
        //从缓存中拿数据
        Map<String, Object> trafficIndexData = SysCacheUtils.get(CACHE_TRAFFIC_DATA);
        //判断是否拿到数据
        if (MapUtil.isEmpty(trafficIndexData)) {
            //从数据库中拿取数据
            trafficIndexData = Static.service.getTrafficIndexData();
            //对数据进行加工
            Map<String, Object> map = new HashMap<>();
            map.put("T1", "平均速度");
            map.put("T2", trafficIndexData.get("平均速度"));
            map.put("T3", "km/h");
            map.put("T4", "交通指数");
            map.put("T5", trafficIndexData.get("交通指数"));
            trafficIndexData = map;
            SysCacheUtils.put(CACHE_TRAFFIC_DATA, trafficIndexData);
        }
        return trafficIndexData;
    }

    public static List<Map<String, Object>> getRegulatoryData() {
        //从缓存中拿取数据
        List<Map<String, Object>> regulatoryData = SysCacheUtils.get(CACHE_REGULATORY_DATA);
        if (CollUtil.isEmpty(regulatoryData)) {
            regulatoryData = CollUtil.newLinkedList();
            //从数据库中拿取数据
            List<Map<String, Object>> dryRegulatoryData = Static.service.getDryRegulatoryData();
            List<Map<String, Object>> drgRegulatoryData = Static.service.getDrgRegulatoryData();
            handleDryData(regulatoryData, dryRegulatoryData);
            handleDrgData(regulatoryData, drgRegulatoryData);
            //存入缓存
            SysCacheUtils.put(CACHE_REGULATORY_DATA, regulatoryData);
        }
        return regulatoryData;
    }

    private static void handleDryData(List<Map<String, Object>> container, List<Map<String, Object>> data) {
        //遍历数据
        for (Map<String, Object> map : data) {
            //获取键值对集合
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                //存放
                Map<String, Object> ans = new HashMap<>();
                //管制情况的name
                String name = entry.getKey();
                //对应的count
                String count = entry.getValue().toString();
                //单位
                String unit = "";
                switch (name) {
                    case "拥堵次数":
                        unit = "次";
                        break;
                    case "拥堵时长":
                        unit = "小时";
                        break;
                    case "拥堵里程":
                        unit = "km";
                        break;
                }
                ans.put("name", name);
                ans.put("data", count);
                ans.put("unit", unit);
                //放入列表
                container.add(ans);
            }
        }
    }

    private static void handleDrgData(List<Map<String, Object>> container, List<Map<String, Object>> data) {
        //遍历数据
        for (Map<String, Object> map : data) {
            //获取键值对集合
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                //存放
                Map<String, Object> ans = new HashMap<>();
                //管制情况的name
                String name = entry.getKey();
                //对应的count
                String count = entry.getValue().toString();
                //单位
                String unit;
                if (name.equals("管制时长")) {
                    unit = "小时";
                } else {
                    unit = "次";
                }
                ans.put("name", name);
                ans.put("data", count);
                ans.put("unit", unit);
                //放入列表
                container.add(ans);
            }
        }
    }

    /**
     * 根据关键字查询大屏数据
     *
     * @param key1 模块名
     * @param key2 子模块名
     * @param key3 子子模块名
     * @return 结果
     */
    public static List<Map<String, String>> getOfficeDataScreenList(String key1, String key2, String key3, String key4) {
        List<Map<String, String>> dataScreenListByKeys = CacheUtils.get(CACHE_OFFICE_DATA_SCREEN_LIST_BY_KEYS, key1 + key2 + key3 + key4);
        if (CollUtil.isEmpty(dataScreenListByKeys)) {
            dataScreenListByKeys = CollUtil.newArrayList();
            List<Map<String, String>> dataScreenList = SysCacheUtils.get(CACHE_OFFICE_DATA_SCREEN_ALL_LIST);
            if (dataScreenList == null) {
                dataScreenList = Static.service.getDataScreen();
                SysCacheUtils.put(CACHE_OFFICE_DATA_SCREEN_ALL_LIST, dataScreenList);
            }
            for (Map<String, String> map : dataScreenList) {
                String key1Copy = map.get("key1");
                String key2Copy = map.get("key2");
                String key3Copy = map.get("key3");
                String key4Copy = map.get("key4");
                if (key1.equals(key1Copy) && key2.equals(key2Copy) && ((StrUtil.isBlank(key3) && StrUtil.isEmpty(key3Copy)) || key3.equals(key3Copy)) && ((StrUtil.isBlank(key4) && StrUtil.isEmpty(key4Copy)) || key4.equals(key4Copy))) {
                    dataScreenListByKeys.add(map);
                }
            }
            CacheUtils.put(CACHE_OFFICE_DATA_SCREEN_LIST_BY_KEYS, key1 + key2 + key3 + key4, dataScreenListByKeys);
        }
        return dataScreenListByKeys;
    }

    public static List<Map<String, String>> getDataScreenAllList() {
        List<Map<String, String>> dataScreenList = SysCacheUtils.get(CACHE_OFFICE_DATA_SCREEN_ALL_LIST);
        if (dataScreenList == null) {
            dataScreenList = Static.service.getDataScreen();
            SysCacheUtils.put(CACHE_OFFICE_DATA_SCREEN_ALL_LIST, dataScreenList);
        }
        return dataScreenList;
    }

    /**
     * 清理大屏数据缓存
     */
    public static void clearCache() {
        SysCacheUtils.remove(CACHE_OFFICE_DATA_SCREEN_ALL_LIST);
        CacheUtils.removeCache(CACHE_START_PROJECT_MILEAGE_DATA);
        CacheUtils.removeCache(CACHE_OPEN_PROJECT_MILEAGE_DATA);
        CacheUtils.removeCache(CACHE_CONSTRUCTION_MILEAGE_DATA);
        CacheUtils.removeCache(CACHE_HIGHWAY_ENTRANCE_FLOW_DATA);
        CacheUtils.removeCache(CACHE_HIGHWAY_EXIT_FLOW_DATA);
        CacheUtils.removeCache(CACHE_ROAD_NETWORK_DATA);
        CacheUtils.removeCache(CACHE_INVESTMENT_DATA);
        CacheUtils.removeCache(CACHE_EMERGENCIES_DATA);
        CacheUtils.removeCache(CACHE_TRAFFIC_DATA);
        CacheUtils.removeCache(CACHE_REGULATORY_DATA);
    }

    private static final class Static {
        private static final OfficeDataScreenService service = SpringUtils.getBean(OfficeDataScreenService.class);
    }

}
